package com.qf.mybatisplus.controller;

import com.qf.mybatisplus.utils.StringUtils;
import com.qf.mybatisplus.websocket.WebsocketClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/webSocket")
public class WebSocketController {


    @RequestMapping(value = "/socket",method = RequestMethod.POST)
    @ResponseBody//此注解不能省略 否则ajax无法接受返回值
    public void login(HttpServletRequest request) throws UnsupportedEncodingException {
          //调用对象webSocketConnect中连接方法
         // webSocketConnect.ClientConnect(request);
        WebsocketClient.socket(request);
        long start = System.currentTimeMillis();
        while (true){
            if (!StringUtils.isEmpty(WebsocketClient.getSession().getAttribute("msg"))){
                System.err.println("收到服务器消息："+WebsocketClient.getSession().getAttribute("msg"));
                WebsocketClient.getSession().removeAttribute("msg");
                break;
            }
        }
        long end = System.currentTimeMillis();
        System.err.println("时间:"+(end-start));
    }
}
